// request.js

const BASE_URL = 'http://localhost:3000/api'; // 你的基础URL

function request(options) {
    // 默认配置
    const defaultOptions = {
        url: '',
        method: 'GET',
        data: {},
        header: {
            'Content-Type': 'application/json'
        }
    };

    // 合并默认配置和传入的配置选项
    const newOptions = Object.assign({}, defaultOptions, options);

    // 显示加载提示
    // uni.showLoading({
    //     title: '加载中...'
    // });
    console.log(newOptions)
    console.log('=====================================',BASE_URL + newOptions.url)
    return new Promise((resolve, reject) => {
        uni.request({
            ...newOptions,
            url: BASE_URL + newOptions.url,
            success(res) {
                console.log(res)
                if (res.statusCode === 200) {
                    resolve(res.data);
                    if(res.data.code !== 200){
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none',
                            mask: true
                        })
                    }
                } else {
                    console.log('-----------------------eeeeeeeeeeeeeeee')
                    uni.showToast({
                        title: res.msg,
                        icon: 'nome',
                        mask: true
                    })
                    reject({ message: '请求失败', error: res });
                }
            },
            fail(err) {
                reject({ message: '请求出错', error: err });
            },
            complete() {
                console.log('请求完成')
                // 隐藏加载提示
                uni.hideLoading();
            }
        });
    });
}

// 导出各种HTTP方法
export function get(url, params = {}) {
    return request({
        url: url,
        method: 'GET',
        data: params
    });
}

export function post(url, data = {}) {
    return request({
        url: url,
        method: 'POST',
        data: data
    });
}

export function put(url, data = {}) {
    return request({
        url: url,
        method: 'PUT',
        data: data
    });
}

export function del(url, params = {}) {
    return request({
        url: url,
        method: 'DELETE',
        data: params
    });
}